package com.swpu.service_archive.controller;


import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.commonutils.ResultEncryption;
import com.swpu.service_archive.entity.DiagnoseRecord;
import com.swpu.service_archive.entity.VaccineRecord;
import com.swpu.service_archive.service.DiagnoseRecordService;
import com.swpu.service_user.aop.Decrypt;
import com.swpu.service_user.aop.Encrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chaichai
 * @since 2024-03-19
 */
@RestController
@RequestMapping("/service_archive/diagnose-record")
public class DiagnoseRecordController {

    @Autowired
    private DiagnoseRecordService diagnoseRecordService;
    @PostMapping("addDiagnoseData")
    @Decrypt
    @Encrypt
    public R addDiagnoseData(@RequestBody ResultEncryption resultEncryption, DiagnoseRecord diagnoseRecord, HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        diagnoseRecordService.addDiagnoseData(openid,diagnoseRecord);
        return R.ok();
    }
    @GetMapping("getDiagnoseList")
    public R getDiagnoseList(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        List<DiagnoseRecord> diagnoseList = diagnoseRecordService.getDiagnoseList(openid);
        return R.ok().data("diagnoseList",diagnoseList);
    }
    @GetMapping("getDiagnoseDataById/{id}")
    public R getVaccineDataById(HttpServletRequest request,@PathVariable String id){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        DiagnoseRecord diagnoseRecord = diagnoseRecordService.getDiagnoseDataById(id);
        return R.ok().data("diagnoseRecord",diagnoseRecord);
    }

    @PostMapping("/updateUserDiagnoseData")
    @Decrypt
    @Encrypt
    public R updateUserVaccineData(@RequestBody ResultEncryption resultEncryption, DiagnoseRecord diagnoseRecord,HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        diagnoseRecordService.updateUserDiagnoseData(diagnoseRecord);
        return R.ok();
    }
    @GetMapping("getDiagnoseRecordCount")
    public R getVaccineRecordCount(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        int diagnoseRecordCount = diagnoseRecordService.getDiagnoseRecordCount(openid);
        return R.ok().data("diagnoseRecordCount",diagnoseRecordCount);
    }
    @DeleteMapping("deleteDiagnoseById/{id}")
    public R deleteVaccineById(HttpServletRequest request,@PathVariable String id){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("用户未登录");
        }
        diagnoseRecordService.deleteDiagnoseById(id);
        return R.ok();
    }
}

